package com.aco.party.web.controller.system;

import com.aco.party.model.po.Terms;
import com.aco.party.model.vo.TermsVo;
import com.aco.party.model.vo.common.DataGridEntry;
import com.aco.party.model.vo.common.Page;
import com.aco.party.model.vo.common.PageParam;
import com.aco.party.model.vo.common.Response;
import com.aco.party.service.system.TermsService;
import com.aco.party.service.system.MetaService;
import com.aco.party.web.command.system.EditTermsCommand;
import com.aco.party.web.validator.system.EditTermsValidator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import static com.aco.party.util.WebModelUtil.*;
/**
 * Created by flying on 3/10/2016 0010.
 */
@Controller
@RequestMapping("/system")
public class ManageTermsController extends BaseController {
    Logger logger = LoggerFactory.getLogger(ManageTermsController.class);

    @Resource
    private TermsService termsService;

    @Resource
    private MetaService metaService;

    private EditTermsValidator validator = new EditTermsValidator();

    private final String REDIRECTEDIT = "redirect:edit";

    @RequestMapping(value = {"/terms", "/terms/list"}, method = RequestMethod.GET)
    public String showManagePage(HttpServletResponse response) {
        return "system/manageTerms";
    }

    @RequestMapping(value = "terms/list", method = RequestMethod.POST)
    @ResponseBody
    public Page
            <TermsVo> getListJson(HttpServletRequest request, DataGridEntry entry) {
        Page <TermsVo> page = new Page<>();
        entry.webParamConvert();
        List <TermsVo> list = termsService.getByPage(new PageParam(entry.getOffset(), entry.getLimit()));
        page.setTotal(termsService.getTotal());
        page.setRows(list);
        return page;
    }

    @RequestMapping(value = "/terms/edit", method = RequestMethod.GET)
    public String showEdit(HttpServletResponse response,@ModelAttribute EditTermsCommand command, Model model) {
        if (command.getId() != null) {
            Terms po = termsService.getByPrimaryKey(command.getId());
            if (po != null)
                command.toCommand(po);
            else
                model.addAttribute("no_obj", true);
        }
        return "system/editTerms";
    }

    @RequestMapping(value = "/terms/edit", method = RequestMethod.POST)
    public String edit(Model model, @ModelAttribute EditTermsCommand command, BindingResult errors) {
        validator.validate(command, errors);
        setAtt(model);
        if (!errors.hasErrors()) {
            Terms terms = new Terms();
                command.toPo(terms);
            termsService.update(terms);
            model.addAttribute("no_error",true);
        }
        return "system/editTerms";
    }

    @RequestMapping(value = "/terms/add", method = RequestMethod.GET)
    public String showAdd(@ModelAttribute EditTermsCommand command) {
        return "system/editTerms";
    }

    @RequestMapping(value = "/terms/add", method = RequestMethod.POST)
    public String add(Model model, @ModelAttribute EditTermsCommand command, BindingResult errors,RedirectAttributes attributes) {
        validator.validate(command, errors);
        setAtt(model);
        if (errors.hasErrors()) {
            return "system/editTerms";
        }
        Terms terms = new Terms();
        command.toPo(terms);
        termsService.add(terms);
        attributes.addAttribute("id",terms.getId());
        attributes.addFlashAttribute("no_error",true);
        return REDIRECTEDIT;
    }

    @RequestMapping(value = "/terms/delete",method = RequestMethod.POST)
    @ResponseBody
    public Response del(@RequestParam(required = true) Long id){
        logger.info("删除：" + id);
        termsService.delete(id);
        return new Response();
    }

    private void setAtt(Model model){

    }

}
